import org.junit.Test;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: zhenzhong.wang
 * @Time: 2018/5/9 11:30
 */
public class AliJavaSpecification {

  @Test
  public void testOne() {

    // 精度丢失
    System.out.println(0.05 + 0.01);
    System.out.println(1.0 - 0.42);
    System.out.println(4.015 * 100);
    System.out.println(123.3 / 100);
    System.out.println(123.1 + 1.1);
    System.out.println(0.1 * 3);
  }

  @Test
  public void testTwo() {
    // 为保证精确，下列应该使用BigDecimal.valueOf(String)方法，而不是BigDecimal.valueOf(double)
    BigDecimal bd1 = BigDecimal.valueOf(123.1);
    BigDecimal bd2 = BigDecimal.valueOf(1.1);
    BigDecimal sum = bd1.add(bd2);
    System.out.println(sum);
  }

  /**
   * List等集合中删除元素需要使用iterator的remove()
   */
  @Test
  public void testThree() {
    ArrayList list = new ArrayList(Arrays.asList(1, 2, 3));
    Iterator<Integer> iterator = list.iterator();
    while (iterator.hasNext()) {
      Integer next = iterator.next();
      if (next.equals(2)) {
        iterator.remove();
      } else {
        System.out.println(next);
      }
    }
  }

  /**
   * 测试线程池创建
   */
  @Test
  public void testFour() {

//    ExecutorService executor = Executors.newFixedThreadPool(100);

    // 使用ThreadPoolExecutor创建线程池，而不是使用JDK提供的现有线程池，详情见阿里java编程规约
    ExecutorService executor = new ThreadPoolExecutor(100, 1000, 0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingDeque<>(100));

    for (int i = 0; i < 1000; i++) {
      Thread thread = new Thread(() -> {
        System.out.println(UUID.randomUUID());
      });
      executor.execute(thread);
    }
  }

  @Test
  public void test() {
    System.out.println(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
  }
}
